package com.luoying.itew.activity;

import android.content.res.Resources;
import android.os.Build;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.appcompat.app.ActionBar;

import com.bumptech.glide.Glide;
import com.luoying.itew.R;

/**
 * Created by lufan on 2019-06-19
 * Class describe
 */
public class PhotoPreviewActivity extends BaseActivity implements View.OnClickListener {
    private ImageView imageView;
    private LinearLayout linearLayout;
    //加载参数 可能是uri 可能path
    private Object loadParam;
    //来源 0 上传图片 1 巡查日志
    private int from;

    @Override
    public int getLayoutResId() {
        return R.layout.activity_preview;
    }

    @Override
    protected void init() {
        super.init();
        imageView = findViewById(R.id.photo_preview_view);
        linearLayout = findViewById(R.id.photo_title_layout);
        findViewById(R.id.bacck_img).setOnClickListener(this::onClick);
        findViewById(R.id.preview_root_view).setOnClickListener(this::onClick);
        from = getIntent().getIntExtra("from",0);
        if(from==0){
            loadParam = this.getIntent().getParcelableExtra("img_uri");
        }
        if(from==1){
            loadParam = this.getIntent().getStringExtra("path");
        }
        if(loadParam==null){
            return;
        }
        Glide.with(this)
                .load(loadParam)
                .into(imageView);

        if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        linearLayout.post(new Runnable() {
            @Override
            public void run() {
                RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,dip2px(60));
                params.setMargins(0,getStatusBarHeight(),0,0);
                linearLayout.setLayoutParams(params);
            }
        });

    }

    //获取状态栏高度
    public  int getStatusBarHeight() {
        Resources resources = this.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        return resources.getDimensionPixelSize(resourceId);
    }

    public  int dip2px(float dpValue) {
        final float scale = this.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
    @Override
    protected void InitActionBar() {
        super.InitActionBar();
        ActionBar actionBar =  getSupportActionBar();
        if(actionBar != null ) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
            case R.id.bacck_img:
                finish();
                break;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.bacck_img){
            finish();
        }
        if(v.getId()==R.id.preview_root_view){
            if(linearLayout.getVisibility()==View.GONE){
                //显示状态栏
                linearLayout.setVisibility(View.VISIBLE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE);
                }
            } else {
                //隐藏状态栏
                linearLayout.setVisibility(View.GONE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
                }
            }
        }
    }
}
